package org.istt.web;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.istt.domain.Category;
import org.istt.domain.Department;
import org.istt.domain.Field;
import org.istt.service.DepartmentManager;
import org.istt.service.FieldManager;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;

public class FieldAction extends BaseAction<FieldManager, Field, Long> {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final Log log = LogFactory.getLog(FieldAction.class);
    private DepartmentManager depMgr;
    private Field field;
    private Long id;
	private String name;
	private String description;
	private String depId = "";
	private List<Department> departments;

	public void setId(Long id) {
		this.id = id;
	}
	@Override
	public Long getId() {
		return (field==null? id : field.getId());//id
	}

	@Autowired
	public void setDepMgr(DepartmentManager depMgr) {
		this.depMgr = depMgr;
	}

	public DepartmentManager getDepMgr() {
		return depMgr;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public List<Department> getDepartments() {
		return this.depMgr.getList();
	}

	public void setDepId(String depId) {
		this.depId = depId;
	}

	public String getDepId() {
		if (entity != null && entity.getDepartment()!=null) {
			depId = entity.getDepartment().getId() + "";
		}
		return depId;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		if (entity != null) {
			description = entity.getDescription();
		}
		return description;
	}

	public void setField(Field field) {
		this.field = field;
	}
	public Field getField() {
		if (entity!= null && entity.getId() != null) {
			return entity;
		}		
		return field;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		if (entity != null) {
			name = entity.getName();
		}
		return name;
	}
	
	@Override
	protected void setSaveMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("field.saved"));
	}

	@Override
	protected void setDeleteMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("field.deleted"));
	}


	@Override
	public String save() {
		if (log.isDebugEnabled()) {
            log.debug("entering 'save' method");
        }
		Department department = new Department();
		if(this.depId!=null && !"".equals(this.depId)){
			department = depMgr.get(Long.parseLong(this.depId));
			
			field.setDepartment(department);
		}
		
        ((FieldManager)mgr).save(field);
        
        setSaveMessage();
                
        return SUCCESS;
	}
	
	private String cancel;
	private String delete;
	private String search;
	
	/**
	 * @return the search
	 */
	public String getSearch() {
		return search;
	}

	/**
	 * @param search the search to set
	 */
	public void setSearch(String search) {
		this.search = search;
	}
	
	public String getCancel() {
       return this.cancel;
    }
	
	public void setCancel(String cancel) {
        this.cancel = cancel;
    }

    public void setDelete(String delete) {
        this.delete = delete;
    }
    
    public String getDelete() {
		return delete;
	}
	
	@Override
	public String execute() {
        if (cancel != null) {
            return "cancel";
        }
        
        if (delete != null) {
            return delete();
        }
        
        if (search != null) {
            return "search";
        }
        
        return SUCCESS;
    }
	
	//Added by NDTHO.2011.12.09
	@Override
	public void validate() {
			boolean result = ((FieldManager)mgr).isUnique(field.getName(), field.getId());
			
			if (!result) {// create new entity
				addActionError(getText("field.name.existed"));
			}
			
			if(this.depId!=null && !"0".equals(this.depId)){
				Department dept = this.depMgr.get(Long.parseLong(depId));
				if(dept.getFields()!=null && dept.getFields().size()!=0){
					Iterator it = dept.getFields().iterator();
					if(it.hasNext()){
						Field fd = (Field)it.next();
						if(fd.getId() != field.getId())
							addActionError(getText("field.department.existed")); 
					}
					
				}
			}
	}
	
	@Override
	public String delete() {
		if (log.isDebugEnabled()) {
            log.debug("entering 'delete' method");
        }
		if (getId() != null) {
			Field fieldObj = mgr.get(getId()) ;
			Set<Category> categories = fieldObj.getCategories();
			if(categories!=null && categories.size()!=0){
				//Delete execution
			    ActionContext.getContext().getSession().put("message", getText("field.delete.error"));
				log.debug("entering 'delete error'");			
			}else{
				log.debug("entering 'delete successful'");			
				mgr.remove(getId());
			}
		}else{
			log.debug("entity is null");
		}
        return "delete";
    }
	
}
